package com.app.service.impl;

import cn.hutool.core.util.StrUtil;
import com.app.mapper.EnjoyEnterpriseMapper;
import com.app.model.AdminUserModel;
import com.app.model.EnjoyEnterprise;
import com.app.service.EnjoyEnterpriseService;
import com.app.vo.EnjoyEnterpriseVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 教程 服务实现类
 * </p>
 *
 * @author shaokuiling
 * @since 2024-10-15
 */
@Service
public class EnjoyEnterpriseServiceImpl extends ServiceImpl<EnjoyEnterpriseMapper, EnjoyEnterprise> implements EnjoyEnterpriseService {
    @Autowired
    private EnjoyEnterpriseMapper enjoyEnterpriseMapper;

    @Override
    public EnjoyEnterpriseVo getEnjoyEnterpriseList(Integer page, Integer limit, String name, AdminUserModel adminUserModel) {
        Page<EnjoyEnterprise> page1 = new Page<>(page, limit);
        QueryWrapper<EnjoyEnterprise> qw = new QueryWrapper<>();
        if (StrUtil.isNotBlank(name)) {
            qw.like("name", name);
        }
        if (adminUserModel != null && adminUserModel.getUserType() == 6) {
            qw.eq("eid", adminUserModel.getEnterpriseId());
        }
        //人社局查询全部
        //银行-查询人社局审核通过的
        if (adminUserModel != null && adminUserModel.getUserType() == 8) {
            qw.eq("human_status", 1);
        }
        qw.orderByDesc("create_time");
        Page<EnjoyEnterprise> enjoyEnterprisePage = enjoyEnterpriseMapper.selectPage(page1, qw);
        EnjoyEnterpriseVo courseVo = new EnjoyEnterpriseVo();
        courseVo.setList(enjoyEnterprisePage.getRecords());
        courseVo.setCount((int) enjoyEnterprisePage.getTotal());
        return courseVo;
    }

    @Override
    public EnjoyEnterpriseVo getEnjoyEnterprises(Integer page, Integer limit, String name, Integer eid) {
        Page<EnjoyEnterprise> page1 = new Page<>(page, limit);
        QueryWrapper<EnjoyEnterprise> qw = new QueryWrapper<>();
        if (StrUtil.isNotBlank(name)) {
            qw.like("name", name);
        }
        if (eid != null && eid != 0) {
            qw.eq("eid", eid);
        }
        qw.orderByDesc("create_time");
        Page<EnjoyEnterprise> enjoyEnterprisePage = enjoyEnterpriseMapper.selectPage(page1, qw);
        List<EnjoyEnterprise> records = enjoyEnterprisePage.getRecords();
        if (records != null && !records.isEmpty()) {
            for (EnjoyEnterprise record : records) {
                if (StringUtils.isNotBlank(record.getPhone())) {
                    record.setPhone(maskPhoneNumber(record.getPhone()));
                }
                if (StringUtils.isNotBlank(record.getEphone())) {
                    record.setEphone(maskPhoneNumber(record.getEphone()));
                }
            }
        }
        EnjoyEnterpriseVo courseVo = new EnjoyEnterpriseVo();
        courseVo.setList(enjoyEnterprisePage.getRecords());
        courseVo.setCount((int) enjoyEnterprisePage.getTotal());
        return courseVo;
    }

    public static String maskPhoneNumber(String phoneNumber) {
        if (StringUtils.isBlank(phoneNumber)) {
            return phoneNumber;
        }
        int length = phoneNumber.length();
        return phoneNumber.substring(0, 3) + "****" + phoneNumber.substring(length - 4);
    }


}
